<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    //for
    // const arr = [1, 2, 3];
    // for (let i = 0; i < arr.length; i++) {
    //   console.log(arr[i]);
    // }
    //for in 
    const arr = [1, 2, 3];
    let index;
    for (index in arr) {
      console.log("arr[" + index + "] = " + arr[index]); //arr[0] = 1 
    }
    
    //for of //ES6

    //forEach()//ES5 //对数组进行遍历循环，对数组中的每一项运行给定函数。这个方法没有返回值。参数都是function类型，默认有传参，参数分别为：遍历的数组内容；第对应的数组索引，数组本身。

    var array = [1, 2, 3, 4, 5];
    array.forEach(function (value, index, arr) {
      console.log(value + '-' + index + '-' + (arr === array))
      console.log(arr)
    })

    array11=[{type:"a", id:1},{type:"b", id:2},{type:"c", id:3}]
    array11.forEach((item) => {
      console.log(item)//{type:"a", id:1}
    })

    var arr1 = [0, 2, 4, 6, 8]
    var sum = 0;
    var str = arr1.forEach(function (item, index, arr1) {
      sum = sum + item;
      console.log("sum", sum);
    })
    console.log("sum的值为：", sum);
  </script>
</body>

</html>